package com.best4cloud.user.domain;

import com.best4cloud.common.database.base.BaseAuditingEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Data
@Entity
@Table(name = "sys_role")
@SQLDelete(sql = "update sys_role set deleted = 1 where id = ?")
@Where(clause = "deleted = 0")
public class SysRole extends BaseAuditingEntity {

    /**
     * 角色名称
     */
    @Column(nullable = false)
    private String roleName;

    /**
     * 角色标识
     */
    @Column(nullable = false, unique = true)
    private String roleCode;

    /**
     * 角色描述
     */
    @Column(nullable = false)
    private String roleDesc;

    /**
     * 逻辑删除
     */
    private boolean deleted;

    /**
     * 菜单列表
     */
    @ManyToMany
    @JoinTable(
            name = "roles_menus",
            joinColumns = {
                    @JoinColumn(name = "role_id",referencedColumnName = "id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "menu_id",referencedColumnName = "id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
            },
            foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
    )
    private Set<SysMenu> menus = new HashSet<>();

    /**
     * 部门列表
     */
    @ManyToMany
    @JoinTable(
            name = "roles_depts",
            joinColumns = {
                    @JoinColumn(name = "role_id",referencedColumnName = "id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
            },
            inverseJoinColumns = {
                    @JoinColumn(name = "dept_id",referencedColumnName = "id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
            },
            foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)
    )
    private Set<SysDept> depts = new HashSet<>();

    public void addMenu(SysMenu sysMenu) {
        menus.add(sysMenu);
    }

    public void addDept(SysDept sysDept) {
        depts.add(sysDept);
    }

}
